package com.icode.web.controller.house;

import com.icode.core.dto.house.ForEnumDTO;
import com.icode.core.dto.house.HouseBaseDTO;
import com.icode.core.dto.order.PageDTO;
import com.icode.core.model.house.BuildingType;
import com.icode.core.model.house.HouseBase;
import com.icode.core.model.house.HouseBaseArea;
import com.icode.core.model.house.HouseBaseTheme;
import com.icode.core.shared.DataToJson;
import com.icode.service.house.HouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: xianfei
 * Date: 15-1-13
 * Time: 上午11:36
 * To change this template use File | Settings | File Templates.
 */
@Controller
@RequestMapping("/house/houseBase")
public class HouseBaseController {

    @Autowired
    private HouseService houseService;

    @RequestMapping(value = "form", method = RequestMethod.GET)
    public ModelAndView showForm(Model model) {
        List<ForEnumDTO> areas = HouseBaseArea.toDTOs();
        List<ForEnumDTO> themes = HouseBaseTheme.toDTOs();
        model.addAttribute("themes", themes);
        model.addAttribute("areas", areas);
        return new ModelAndView("back/houseBase/houseBaseUpdate", "houseBaseDTO", new HouseBaseDTO());
    }

    @RequestMapping(value = "list/{currentPage}", method = RequestMethod.GET)
    public String showHouseBaseList(Model model, @PathVariable String currentPage) {
        String totalPage = houseService.findTotalPage(HouseBase.class).toString();
        currentPage = PageDTO.isPage(currentPage, totalPage);
        List<HouseBaseDTO> houseBaseDTOs = houseService.findHouseBase(currentPage);
        model.addAttribute("houseBaseList", houseBaseDTOs);
        model.addAttribute("totalPage", totalPage);
        model.addAttribute("currentPage", currentPage);
        return "back/houseBase/houseBaseOverView";
    }

    @RequestMapping(value = "beforeUpdate/{guid}", method = RequestMethod.GET)
    public ModelAndView BeforeUpdate(@PathVariable String guid, Model model) {
        HouseBaseDTO houseBaseDTO = houseService.findHouseBaseByGuid(guid);
        List<ForEnumDTO> areas = HouseBaseArea.toDTOs();
        List<ForEnumDTO> themes = HouseBaseTheme.toDTOs();
        model.addAttribute("areas", areas);
        model.addAttribute("themes", themes);
        return new ModelAndView("back/houseBase/houseBaseUpdate", "houseBaseDTO", houseBaseDTO);
    }

    @RequestMapping(value = "update", method = RequestMethod.POST)
    public String update(@ModelAttribute(value = "houseBaseDTO") @Valid HouseBaseDTO houseBaseDTO, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return "back/houseBase/houseBaseUpdate";
        }
        if ("".equals(houseBaseDTO.getGuid())) {
            houseService.saveOrUpdateHouseBase(houseBaseDTO);
        } else {
          //  houseService.deleteHouseBaseImage(houseBaseDTO);
            houseService.saveOrUpdateHouseBaseByGuid(houseBaseDTO, houseBaseDTO.getGuid());
        }
        return "redirect:/house/houseBase/beforeUpdate/"+houseBaseDTO.getGuid();
    }

    @RequestMapping(value = "delete", method = RequestMethod.POST)
    public String delete(String guid) {
        houseService.deleteHouseBaseByGuid(guid);
        return "redirect:/house/houseBase/list/1";
    }

    @RequestMapping(value = "beforeDelete", method = RequestMethod.GET)
    public void beforeDelete(String guid, HttpServletResponse response) {
        List<BuildingType> buildingTypes = houseService.findBuildingTypeByHouseBase(guid);
        if (!buildingTypes.isEmpty()) {
            String content = "false";
            DataToJson.DataToJson(content, response);
        } else {
            DataToJson.DataToJson("true", response);
        }
    }

    @RequestMapping(value = "delImg", method = RequestMethod.POST)
    public void deleteUploadImage(String imgGuid, HttpServletResponse response) {
        houseService.deleteUploadImageByGuid(imgGuid);
        String content = "success";
        DataToJson.DataToJson(content, response);
    }

    @RequestMapping(value = "houseBaseArea", method = RequestMethod.GET)
    public void houseBaseArea(HttpServletResponse response) {
        List<ForEnumDTO> list = HouseBaseArea.toDTOs();
        DataToJson.houseBaseAreaToJson(list, response);
    }

    @RequestMapping(value = "/sort/{sort}/{guid}")
    public void houseBaseSort(@PathVariable int sort, @PathVariable String guid) {
        houseService.sort(sort, guid);
    }


}
